package com.ks.BusAlarm;

import java.io.InputStream;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.os.Vibrator;
import android.speech.tts.TextToSpeech;
import android.widget.Toast;

public class MyService extends Service {
	private TextToSpeech mTts;

	Context context;
	String BusStopNum, BusNum;
	int TTSleftTime;

	Timer timer;

	SharedPreferences sp;
	Boolean PrefVibration;
	Boolean PrefSound;
	int PrefTimer;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub

		return null;
	}

	@Override
	public void onCreate() {

		context = this;
		Toast.makeText(context, "시작", Toast.LENGTH_LONG).show();
		mTts = new TextToSpeech(this, null);
		mTts.setLanguage(Locale.US);

		timer = new Timer();

		super.onCreate();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		sp = getSharedPreferences(getString(R.string.PREF_NAME), 0);
		PrefVibration = sp
				.getBoolean(getString(R.string.PREF_VIBRATION), false);
		PrefSound = sp.getBoolean(getString(R.string.PREF_SOUND), false);
		PrefTimer = sp.getInt(getString(R.string.PREF_TIMER), 20);

		BusStopNum = intent.getStringExtra("BusStopNum");
		BusNum = intent.getStringExtra("BusNum");

		Task task = new Task(BusStopNum, BusNum);

		timer.schedule(task, 0, PrefTimer * 1000);

		return START_STICKY;
	}

	public void onDestroy() {

		timer.cancel();
	}

	private void play() {

		String msg;
		if (TTSleftTime == -1) {

			msg = "No Information";

		} else if (TTSleftTime == -100) {
			msg = "preparing bus.";
		} else if (TTSleftTime == -200) {
			msg = "Bus is over.";
		} else {

			msg = String.valueOf(TTSleftTime) + "분 남았습니다.";

		}

		mTts.speak(msg, TextToSpeech.QUEUE_FLUSH, null);
	}

	class Task extends TimerTask {

		String Task_BusStopNum, Task_BusNum;

		Task(String pBusStopNum, String pBusNum) {
			Task_BusStopNum = pBusStopNum;
			Task_BusNum = pBusNum;
		}

		@Override
		public void run() {

			NotificationManager notifier;
			Notification notify;
			Intent toLaunch;
			PendingIntent intentBack;

			notifier = (NotificationManager) context
					.getSystemService(Context.NOTIFICATION_SERVICE);
			notify = new Notification(R.drawable.statusbar_icon2, "도착알림",
					System.currentTimeMillis());
			toLaunch = new Intent(context.getApplicationContext(),
					BusAlarm.class);

			intentBack = PendingIntent.getActivity(
					context.getApplicationContext(), 0, toLaunch, 0);

			notify.flags = notify.flags | Notification.FLAG_AUTO_CANCEL;

			httpClient hp = new httpClient();
			InputStream HTTPData;
			String url = "http://bus.go.kr/?searchType=A3&searchCode=N&searchName=3,"
					+ Task_BusStopNum + "&imagesType=T1";

			ksLog.d("url", url);

			HTTPData = hp.post(url);

			jerichoParser jp = new jerichoParser(Task_BusNum);

			TTSleftTime = jp.parseBusInfo(HTTPData);

			notify.setLatestEventInfo(context.getApplicationContext(), "시작",
					String.valueOf(TTSleftTime), intentBack);

			if (PrefVibration)
				vibrate(TTSleftTime);

			notifier.notify(0x1001, notify);

			if (PrefSound)
				play();

		}

		void vibrate(int pLeftTime) {
			Vibrator vibe = (Vibrator) getSystemService(VIBRATOR_SERVICE);

			ksLog.d("lefttime", pLeftTime);

			switch (pLeftTime) {

			case 1:
				vibe.vibrate(new long[] { 0, 1000 }, -1);
				break;

			case 2:
				vibe.vibrate(new long[] { 200, 200, 200, 200 }, -1);
				break;

			case 3:
				vibe.vibrate(new long[] { 200, 200, 200, 200, 200, 200 }, -1);
				break;

			case 4:
				vibe.vibrate(new long[] { 200, 200, 200, 200, 200, 200, 200,
						200 }, -1);
				break;

			case 5:
				vibe.vibrate(new long[] { 200, 200, 200, 200, 200, 200, 200,
						200, 200, 200 }, -1);
				break;

			case 6:
				vibe.vibrate(new long[] { 200, 200, 200, 200, 200, 200, 200,
						200, 200, 200, 200, 200 }, -1);
				break;

			case 7:
				vibe.vibrate(new long[] { 200, 200, 200, 200, 200, 200, 200,
						200, 200, 200, 200, 200, 200, 200 }, -1);
				break;

			case 8:
				vibe.vibrate(new long[] { 200, 200, 200, 200, 200, 200, 200,
						200, 200, 200, 200, 200, 200, 200, 200, 200 }, -1);
				break;

			case 9:
				vibe.vibrate(
						new long[] { 200, 200, 200, 200, 200, 200, 200, 200,
								200, 200, 200, 200, 200, 200, 200, 200, 200,
								200 }, -1);
				break;

			case 10:
				vibe.vibrate(new long[] { 200, 200, 200, 200, 200, 200, 200,
						200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200,
						200, 200 }, -1);
				break;

			default:
				vibe.vibrate(new long[] { 0, 2000 }, -1);
				break;
			}

		}
	}

}
